<?require_once("connection.php");
  error_reporting(0);
  $course_id = $_POST["course_id"];
  $starting_date = $_POST["starting_date"];
  
  //$date = date_create($starting_date);
  //date_add($date, date_interval_create_from_date_string('7 days'));
  //echo date_format($date, 'Y-m-d');


$str = $starting_date;
$timestamp = strtotime($str);
//echo date("Y/m/d", $timestamp);
if (($timestamp = strtotime($str)) !== false){
  //$php_date = getdate($timestamp);
  // or if you want to output a date in year/month/day format:
  //$date = date("Y/m/d", $timestamp); // see the date manual page for format options
  //echo date("Y/m/d", $timestamp);
  //echo date("m", $timestamp);
  //if(date("m", $timestamp)>=2) echo "ulken";
  //else echo "kishi";
  
}
else
{
  echo 'invalid timestamp!';
}

  $query = "select * from g_courses where course_id=".$course_id.";";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  $name = $row["name"];
  $content = $row["content"];
  $year = date("Y", $timestamp);
  if(date("m", $timestamp)<9)
    $semester = "Spring";
  else
    $semester = "Fall";
    
  $query = "insert into g_courses values('none', '".$name."', '".$year."', '".$semester."', '".$content."');";
  $result = mysql_query($query, $connection);
  
  $query = "select max(course_id) from g_courses;";
  $result = mysql_query($query);
  $a = mysql_fetch_array($result);
  $max_id = $a[0];

  $query1 = "select staff_id from g_staff_course where course_id=".$course_id.";";
  $result1 = mysql_query($query1);
  while($row1=mysql_fetch_array($result1)){
    $query2 = "insert into g_staff_course(course_id,staff_id) value(".$max_id.",".$row1["staff_id"].");";
    $result2 = mysql_query($query2);
  }

  $query1 = "select * from g_lectures where course_id=".$course_id." order by seq_num;";
  $result1 = mysql_query($query1);
  $date = date_create($starting_date);
  while($row1=@mysql_fetch_array($result1)){
    $query2 = "insert into g_lectures values('none', ".$row1["seq_num"].", '".date_format($date, 'Y-m-d')."', '".$row1["description"]."', '".$row1["slide"]."', '".$row1["slide_url"]."', '".$row1["additional"]."', '".$row1["additional_url"]."', ".$max_id.");";
    $result2 = mysql_query($query2);
    date_add($date, date_interval_create_from_date_string('7 days'));
  }

  $query1 = "select * from g_exercises where course_id=".$course_id." order by seq_num;";
  $result1 = mysql_query($query1);
  $date = date_create($starting_date);
  $date1 = date_create($starting_date);
  while($row1=@mysql_fetch_array($result1)){
    date_add($date1, date_interval_create_from_date_string('14 days'));
    $query2 = "insert into g_exercises values('none', ".$row1["seq_num"].", '".date_format($date, 'Y-m-d')."', '".$row1["file"]."', '".$row1["description"]."', '".$row1["solution"]."', '".date_format($date1, 'Y-m-d')."', '".$row1["slide"]."', '".$row1["additional"]."', ".$max_id.");";
    $result2 = mysql_query($query2);
    date_add($date, date_interval_create_from_date_string('14 days'));
  }

  if(empty($result)){
     echo "<pre>The record has not been ADDED!!!</pre>";
  }
  else{
     echo "<pre>The record has been successfully ADDED</pre>";
  }
  require_once("display_courses.php");
?>
